//使用http
const https = require('http')
const options = {
    hostname: '127.0.0.1',
    port: 10086,
    path: '/cards',
    method: 'GET',
    secure: true,
}
let data = JSON.stringify({
    id: 10044,
    name: 'http GET',
    dest: '通过监听流的data事件来获取数据',
    time: new Date().getDate(),
})
const qs = require('querystring')
options['path'] += `?` + qs.stringify(data)
//get
console.log('https GET')
let req = https.request(options, res => {
    console.log(`http GET状态码：${res.statusCode}`)
    res.setEncoding('utf8');
    res.on('data', data => {
        process.stdout.write(`http GET:${data}\n`)
    })
})
req.on('error', error => {
    console.error(`http GET error::${error}`)
})
req.end()

// //post
data['id']=10808
data['name']='http POST'
options['method'] = 'POST'
options['headers'] = {
    'Content-type': 'application/json',
    'Content-Length': data.length,
}
console.log('https POST')
req = https.request(options, (res) => {
    console.log(`http POST状态码：${res.statusCode}`)
    res.on('data', data => {
        process.stdout.write(`http POST::${data}\n`)
    })
})
req.on('error', error => {
    console.error(`http POST error::${error}`)
})
req.write(data)
req.end()

//使用axios
let data2 = JSON.stringify({
    id: 10099,
    name: 'axios',
    dest: 'axios POST'
})
const url = 'http://127.0.0.1:10086/cards'
const axios = require('axios')
console.log('axios POST')
axios
    .post(url, data2)
    .then(res => {
        console.log(`axios状态码：${res.statusCode}`)
        console.log(`axios数据：${res}`)
    })
    .catch(error => {
        console.error(error)
    })